<template>
  <div id="app">
    <router-view />
  </div>
</template>

<script>
export default {
  name: 'App',
  created () {
    const arr = ['ctrl', 'ctrlbid', 'bidnet', 'bidco']
    if (arr.indexOf(process.env.VUE_APP_ROLE) > -1) {
      this.initSocket()
    }
    // this.initSocket()
  },
  methods: {
    initSocket () {
      if ('WebSocket' in window) {
        this.ws = new WebSocket(process.env.VUE_APP_WS_HOST)
        this.global.setWs(this.ws)
        this.ws.onopen = () => {
          console.log('App - WebSocket连接成功!')
          this.global.wsConnected = true
        }
        this.ws.onerror = e => {
          console.log('App - WebSocket连接发生错误!')
          this.global.wsConnected = false
        }
        this.ws.onmessage = e => {
          var data = JSON.parse(e.data)
          console.log(data)
        }
        this.ws.onclose = () => {
          console.log('App - WebSocket连接关闭!')
          this.global.wsConnected = false
        }
      } else {
        // 浏览器不支持 WebSocket
        console.log('您的浏览器不支持 WebSocket!')
      }
    }
  }
}
</script>
